import axios from 'axios'
import store from '../store'
import {Message} from 'element-ui';
import router from '../router'
import wmJson from '../../public/waste_material.json'
import industryJson from '../../public/industry.json'

const AppInit = {}


// AppInit.loadConfig = function() {
//   var baseConfigUrl = process.env.VUE_APP_STATIC_ROOT + 'base_config.js';
//   var navConfigUrl = process.env.VUE_APP_STATIC_ROOT + 'nav.js';
//   var industryConfigUrl = process.env.VUE_APP_STATIC_ROOT + 'industry.json';
//   var wasteMaterialConfigUrl = process.env.VUE_APP_STATIC_ROOT + 'waste_material.json';
//   var downloadUrl = process.env.VUE_APP_STATIC_ROOT + 'download.js';
//   return new Promise((resolve, reject) => {
//       axios.all([axios.get(baseConfigUrl), axios.get(navConfigUrl), axios.get(industryConfigUrl), axios.get(wasteMaterialConfigUrl), axios.get(downloadUrl)])
//           .then(axios.spread(function(_base, _nav, _industry, _wasteMaterial, _download) {
//               resolve({
//                   '_base_config': _base.data,
//                   '_nav_config': _nav.data,
//                   '_industry_config': _industry,
//                   '_wasteMaterial_config': wmJson,
//                   '_download_config': download,
//               });
//           })).catch(error => {
//               reject(error);
//           })
//   });
// }

AppInit.loadConfig = function() {
    let config={
      '_industry_config': industryJson,
      '_wasteMaterial_config': wmJson
    }
    return {
      then(result){
        result(config);
        return {
          catch() {

          }
        }
      }
    }
}

AppInit.axiosInterceptors = function() {
    //请求 - 拦截器
    axios.interceptors.request.use(
        config => {
            if (window.localStorage.getItem('token')) {
                config.headers['Authorization'] = window.localStorage.getItem('token');
            }
            if(config.url.endsWith('login')) {
                if(window.localStorage.getItem('tenantCode')) {
                    config.headers['tenantCode'] = window.localStorage.getItem('tenantCode');
                }
            }
            return config;
        },
        error => {
            //请求错误 - 跳转登录
            router.replace({
                path: '/',
                //todo 跳转首页
                query: { redirect: router.currentRoute.fullPath }
            })
            return Promise.reject(error);
        }
    );
    //响应 - 拦截器
    axios.interceptors.response.use(success => {
        if (success.status && success.status == 200) {
            return success;
        } else if (success.status == 500 || success.status == 401 || success.status == 403) {
            Message.error({message: success.data.msg});
            return false;
        }
        return success;
    }, error => {
        if(!error) return false;
        if (error.status == 504 || error.status == 404) {
            Message.error({message: '服务器异常'});
        } else if (error.status == 403) {
            Message.error({message: '权限不足，请联系管理员！'});
        } else if (error.status == 401) {
            Message.error({message: '尚未登录，请登录'});
            router.replace('/');
        } else {
            if (error.msg) {
                Message.error({message: error.data.msg});
            } else {
                // Message.error({message: '未知错误！'});
            }
        }
        return false;
    });
}

export default AppInit;
